package com.srit.core.bean;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.srit.core.exception.DataNotFoundException;

/**
 * @author guan
 * 
 */
public class SearchParam<T, K> extends HashMap<String, String> {
	private static final String NAME_TYPE_SEP = "_";
	private static final String TYPE_INTEGER = "INTEGER";
	private static final String TYPE_STRING = "STRING";
	private static final String TYPE_BOOLEAN = "BOOLEAN";

	public Map covertFromMap() throws DataNotFoundException {
		Map paramMap = new HashMap();
		for (Iterator<String> it = this.keySet().iterator(); it.hasNext();) {
			String origKey = it.next();
			String value = this.get(origKey);
			int index = origKey.indexOf(NAME_TYPE_SEP);
			if (index == -1) {
				paramMap.put(origKey, value);
			} else {
				paramMap.put(origKey.subSequence(0, index),
						covertValueByType(origKey.substring(index + 1), value));
			}
		}
		return paramMap;
	}

	private Object covertValueByType(String valueType, String value)
			throws DataNotFoundException {
		if (TYPE_INTEGER.indexOf(valueType.toUpperCase()) != -1) {
			return Integer.valueOf(value);
		} else if (TYPE_STRING.indexOf(valueType.toUpperCase()) != -1) {
			return value;
		} else if (TYPE_BOOLEAN.indexOf(valueType.toUpperCase()) != -1) {
			return Boolean.valueOf(value);
		} else {
			throw new DataNotFoundException("类型:" + valueType + "没有匹配");
		}

	}

	public static void main(String[] args) {
		// SearchParam sp=new SearchParam();
		// String[] s={"test","111"};
		// sp.put("aaa",s);
	}
}
